import {
    getArea,
    getRegionArea,
    getAticleType,
    searchDoc
} from "@/api/api.js";

export default {
    data() {
        return {
            checkAll: false,
            isIndeterminate: true,
            aticles: [{
                channelId: '',
                channelName: "全部",
            }, ],
            regions: [],
            citys: [],
            formData: {
                channelId: [''],
                provinceId: [],
                searchType: '1',
                date: '0',
                regionId: [],
                cityId: [],
                isAll: "0",
                year: "",
                month: "",
                day: "",
                isTitle: "-1",
                q: "",
                pageNo: 1,
                pageSize: 20
            },
        }
    },
    computed: {
        regionId() {
          return this.regions.map(({id}) => id)
        }
      },
    mounted() {
        let {
            channelId = [''], 
            date = "0",
            day = "",
            isAll= "0",
            isTitle= "-1",
            month= "",
            pageNo= 1,
            pageSize= 20,
            q= "",
            searchType= "1",
            year= "",
        } = this.$route.query
        Object.assign(this.formData, {
            channelId: channelId ? channelId : [], 
            date ,
            day ,
            isAll ,
            isTitle ,
            month ,
            pageNo: Number(pageNo),
            pageSize ,
            q ,
            searchType,
            year
        })
        this.loadRegionData();
        this.getAticleType();
        console.log('options', this.$route.query)
    },
    methods: {
        loadRegionData() {
            getRegionArea().then((res) => {
                console.log(res);
                this.regions = res.list.map(item => {
                    item.regionAreaList = item.regionAreaList.map((pro, index) => {
                        return {
                            ...pro,
                            children: [{
                                    id: pro.areaId,
                                    label: pro.areaName,
                                    children: [{
                                        id: pro.areaId + '-' + index,
                                        label: index
                                    }]
                                }

                            ]
                        }
                    })
                    return item
                });
            });
        },
        async getAticleType() {
            let {
                list = []
            } = await getAticleType().catch((err) => {});
            if (list.length) {
                this.aticles = this.aticles.concat(list);
            }
        },
        async handleSelectArea(regionId, area) {
            let {list} = await getArea({ level: 2, pid:  area.areaId})
            console.log(list)
            this.citys = list
            if(this.selectRegion == regionId && this.selectArea == area.areaId) {
              this.selectRegion = ''
              this.selectArea = ''
            } else {
              this.selectRegion = regionId
              this.selectArea = area.areaId
            }
            
          },
        handleSearch() {
            this.$router.push({
                path: '/searchResult',
                query: {...this.formData}
            });
            // searchDoc(this.formData)
        },
        handleCheckAllChange(val) {
            this.formData.regionId = val ? this.regionId : [];
            this.isIndeterminate = false;
        },
        handleCheckedRegionChange(value) {
            let checkedCount = value.length;
            this.checkAll = checkedCount === this.regionId.length;
            this.isIndeterminate = checkedCount > 0 && checkedCount < this.regionId.length;
        },
        handleCheckedCitiesChange(value) {
            console.log(value)
        }
    }
}